//
//  AppDelegate.swift
//  weibo
//
//  Created by Fosen on 15/11/26.
//  Copyright © 2015年 fosen. All rights reserved.
//

/*

未登录 ——  显示访客视图  ——   登陆界面  —— 欢迎界面  —— 首页
登录 —— 如果是新版本 —— 新特性界面  —— 首页
     —— 如果不是新版本 —— 欢迎界面 —— 首页

*/


import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
//        let account = UserAccount.loadAccount()
//        print(account)
        //设置主题色
        setThemeColor()
        
        
        regitisterNotifucation()
        //检查是否是新版本
//        if isNewVersion(){
//            print("新版本")
//        
//        } else{
//        
//        
//        }
        //手动实例化window
        window = UIWindow (frame: UIScreen .mainScreen().bounds)
        
        window?.backgroundColor = UIColor.whiteColor()
        
        window?.makeKeyAndVisible()
        
        window?.rootViewController = defaultRootViewController()
        
        return true
    }
    
    private func regitisterNotifucation(){
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "switchRootVC:", name: WebSwitchRootVCNotification, object: nil)
    }
    
    //通知中实现的方法也必须加@objc
    @objc private func switchRootVC(not:NSNotification){
//        if not.object != nil {
//            window?.rootViewController
//            return
//        }
//        window?.rootViewController = MainViewController()
        
        
        window?.rootViewController =  not.object == nil  ? MainViewController() : WelcomeViewController()
    }
    

    private func setThemeColor(){
        UINavigationBar.appearance().tintColor = themeColor
        UITabBar.appearance().tintColor = themeColor
    }
    
    private func defaultRootViewController() ->UIViewController{
    
    
        if UserAccountViewModel().userLogin {
        
            if isNewVersion(){
            
                return NewfeatureViewController()
            }
        
            return WelcomeViewController()
        }
        //如果没有登录
        return MainViewController()
    }
    
    private func isNewVersion() ->Bool{
    
        //获取当前版本
        let dict = NSBundle.mainBundle().infoDictionary!
        let currentVersion = (dict["CFBundleShortVersionString"] as! NSString).doubleValue
        
        //获取上一版本
        //设置key
        let sandBoxKey = "sandBox"
        let defauls = NSUserDefaults.standardUserDefaults()
        let lastVersion = defauls.doubleForKey(sandBoxKey)
        
        //记录当前的系统版本
        defauls.setDouble(currentVersion, forKey: sandBoxKey)
        defauls.synchronize()
        //比较两个版本
        return currentVersion > lastVersion
    }
    
    //析构函数中移除通知
    deinit{
        NSNotificationCenter.defaultCenter().removeObserver(self)

    }
    
    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

